home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / ui / components / example / AppletButton.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  5.9 KB  |  174 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. //TO DO: Close all windows when you leave the page?
  18.  
  19. import java.awt.*;
  20. import java.util.*;
  21. import java.applet.Applet;
  22.  
  23. public class AppletButton extends Applet implements Runnable {
  24.     int frameNumber = 1;
  25.     String windowClass;
  26.     String buttonText;
  27.     String windowTitle;
  28.     int requestedWidth = 0;
  29.     int requestedHeight = 0;
  30.     Button button;
  31.     Thread windowThread;
  32.     Label label;
  33.     boolean pleaseCreate = false;
  34.  
  35.     public void init() {
  36.         windowClass = getParameter("WINDOWCLASS");
  37.         if (windowClass == null) {
  38.             windowClass = "TestWindow";
  39.         }
  40.  
  41.         buttonText = getParameter("BUTTONTEXT");
  42.         if (buttonText == null) {
  43.             buttonText = "Click here to bring up a " + windowClass;
  44.         }
  45.  
  46.         windowTitle = getParameter("WINDOWTITLE");
  47.         if (windowTitle == null) {
  48.             windowTitle = windowClass;
  49.         }
  50.  
  51.         String windowWidthString = getParameter("WINDOWWIDTH");
  52.         if (windowWidthString != null) {
  53.             try {
  54.                 requestedWidth = Integer.parseInt(windowWidthString);
  55.             } catch (NumberFormatException e) {
  56.                 //Use default width.
  57.             }
  58.         }
  59.  
  60.         String windowHeightString = getParameter("WINDOWHEIGHT");
  61.         if (windowHeightString != null) {
  62.             try {
  63.                 requestedHeight = Integer.parseInt(windowHeightString);
  64.             } catch (NumberFormatException e) {
  65.                 //Use default height.
  66.             }
  67.         }
  68.  
  69.         setLayout(new GridLayout(2,0));
  70.         add(button = new Button(buttonText));
  71.         button.setFont(new Font("Helvetica", Font.PLAIN, 14));
  72.  
  73.         add(label = new Label("", Label.CENTER));
  74.     }
  75.  
  76.     public void start() {
  77.         if (windowThread == null) {
  78.             windowThread = new Thread(this, "Bringing Up " + windowClass);
  79.             windowThread.start();
  80.         }
  81.     }
  82.  
  83.     public synchronized void run() {
  84.         Class windowClassObject = null;
  85.         Class tmp = null;
  86.         String name = null;
  87.         
  88.         // Make sure the window class exists and is really a Frame.
  89.         // This has the added benefit of pre-loading the class,
  90.         // which makes it much quicker for the first window to come up.
  91.         try {
  92.             windowClassObject = Class.forName(windowClass);
  93.         } catch (Exception e) {
  94.             // The specified class isn't anywhere that we can find.
  95.             label.setText("Can't create window: Couldn't find class "
  96.                               + windowClass);
  97.             button.disable();
  98.             return;
  99.         }
  100.  
  101.         // Find out whether the class is a Frame.
  102.         for (tmp = windowClassObject, name = tmp.getName();
  103.              !( name.equals("java.lang.Object") ||
  104.                 name.equals("java.awt.Frame") ); ) {
  105.             tmp = tmp.getSuperclass();
  106.             name = tmp.getName();
  107.         }
  108.         if ((name == null) || name.equals("java.lang.Object")) {
  109.             //We can't run; ERROR; print status, never bring up window
  110.             label.setText("Can't create window: "
  111.                               + windowClass +
  112.                           " isn't a Frame subclass.");
  113.             button.disable();
  114.             return;
  115.         } else if (name.equals("java.awt.Frame")) { 
  116.             //Everything's OK. Wait until we're asked to create a window.
  117.             while (windowThread != null) {
  118.                 while (pleaseCreate == false) {
  119.                     try {
  120.                         wait();
  121.                     } catch (InterruptedException e) {
  122.                     }
  123.                 }
  124.  
  125.                 //We've been asked to bring up a window.
  126.                 pleaseCreate = false;
  127.                 Frame window = null;
  128.                 try {
  129.                     window = (Frame)windowClassObject.newInstance();
  130.                 } catch (Exception e) {
  131.                     label.setText("Couldn't create instance of class "
  132.                                   + windowClass);
  133.                     button.disable();
  134.                     return;
  135.                 }
  136.                 if (frameNumber == 1) {
  137.                     window.setTitle(windowTitle);
  138.                 } else {
  139.                     window.setTitle(windowTitle + ": " + frameNumber);
  140.                 }
  141.                 frameNumber++;
  142.  
  143.                 //Set the window's size.
  144.                 window.pack();
  145.                 if ((requestedWidth > 0) | (requestedHeight > 0)) {
  146.                     window.resize(Math.max(requestedWidth,
  147.                                            window.size().width),
  148.                                   Math.max(requestedHeight,
  149.                                            window.size().height));
  150.                 }
  151.  
  152.                 window.show();
  153.                 label.setText("");
  154.             }
  155.         }
  156.     }
  157.                 
  158.     public synchronized boolean action(Event event, Object what) {
  159.         if (event.target instanceof Button) {
  160.             //signal the window thread to build a window
  161.             label.setText("Please wait while the window comes up...");
  162.             pleaseCreate = true;
  163.             notify();
  164.         } 
  165.         return true;
  166.     }
  167. }
  168.  
  169. class TestWindow extends Frame {
  170.     public TestWindow() {
  171.         resize(300, 300);
  172.     }
  173. }
  174.